from django.db import models
from db.base_model import BaseModel
from db.base_manager import BaseManager




class CommentManager(BaseManager):
    def add_comment(self, name, email, url, text, post):
        obj = self.create_one_object(name=name, email=email, url=url, text=text, post_id=post)
        return obj

# 评论
class Comment(BaseModel):
    name = models.CharField(max_length=100)
    email = models.EmailField(max_length=255)
    url = models.URLField(blank=True)
    text = models.TextField()
    created_time = models.DateTimeField(auto_now_add=True)

    # post = models.ForeignKey('bl.Blog', on_delete=models.DO_NOTHING) # 不是级联删除，防止删除博客
    post = models.ForeignKey('bl.Blog', on_delete=models.CASCADE) # 不是级联删除，防止删除博客
    objects = CommentManager()
    class Meta:
        db_table = 'comment'
        verbose_name_plural = verbose_name = '评论'
        ordering = ['-created_time']

    def __str__(self):
        return self.text[:20]
